<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.*" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.file.Prop" %>
<%@ page import="weaver.conn.RecordSet"%>
<%@page import="weaver.general.BaseBean"%>
<%@ page import="weaver.login.Base64"%>

<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="rs2" class="weaver.conn.RecordSet" scope="page" />
<%!
private String getLogMessage(String uid){
	String message = "";
	RecordSet rs = new RecordSet();
	String sqltmp = "";
    if (rs.getDBType().equals("oracle")) {
        sqltmp = "select * from (select * from SysMaintenanceLog where relatedid = " + uid + " and operatetype='6' and operateitem='60' order by id desc ) where rownum=1 ";
    }else if(rs.getDBType().equals("db2")){
        sqltmp = "select * from SysMaintenanceLog where relatedid = "+uid +" and operatetype='6' and operateitem='60' order by id desc fetch first 1 rows only ";
    } else {
        sqltmp = "select top 1 * from SysMaintenanceLog where relatedid = " + uid + " and operatetype='6' and operateitem='60' order by id desc";
    }
    rs.executeSql(sqltmp);
    if (rs.next()){
        message = rs.getString("clientaddress") + " " + rs.getString("operatedate") + " " + rs.getString("operatetime");
    }
    return message;
}
%>

<%
String loginid = Util.null2String(request.getParameter("loginid"));
String billid = Util.null2String(request.getParameter("billid"));
String type = Util.null2String(request.getParameter("type"));
String requestid = Util.null2String(request.getParameter("requestid"));
System.out.println("loginid:"+loginid);
if("".equals(loginid)){
	response.sendRedirect("/login/Login.jsp");
	return;
}

String error_code = "";
String msg = "";
String userID = "";

try{
	//userID = new String(Base64.decode(loginid));
	userID = new String(loginid);
}catch (Exception e){
	new BaseBean().writeLog(e);
	response.sendRedirect("/login/Login.jsp");
	return;
}

User user_new = null;

rs.executeSql("select * from HrmResource where loginid='"+userID+"' and status<4");
System.out.println("getsql:"+"select * from HrmResource where id="+userID+" and status<4");

if(rs.next()){
	User user = (User)request.getSession(true).getAttribute("weaver_user@bean") ;
	if(null==user){
		user_new = new User();
		user_new.setUid(rs.getInt("id"));
		user_new.setLoginid(rs.getString("loginid"));
		user_new.setFirstname(rs.getString("firstname"));
		user_new.setLastname(rs.getString("lastname"));
		user_new.setAliasname(rs.getString("aliasname"));
		user_new.setTitle(rs.getString("title"));
		user_new.setTitlelocation(rs.getString("titlelocation"));
		user_new.setSex(rs.getString("sex"));
		user_new.setPwd(rs.getString("password"));
		String languageidweaver = rs.getString("systemlanguage");
		user_new.setLanguage(Util.getIntValue(languageidweaver, 0));
		user_new.setTelephone(rs.getString("telephone"));
		user_new.setMobile(rs.getString("mobile"));
		user_new.setMobilecall(rs.getString("mobilecall"));
		user_new.setEmail(rs.getString("email"));
		user_new.setCountryid(rs.getString("countryid"));
		user_new.setLocationid(rs.getString("locationid"));
		user_new.setResourcetype(rs.getString("resourcetype"));
		user_new.setStartdate(rs.getString("startdate"));
		user_new.setEnddate(rs.getString("enddate"));
		user_new.setContractdate(rs.getString("contractdate"));
		user_new.setJobtitle(rs.getString("jobtitle"));
		user_new.setJobgroup(rs.getString("jobgroup"));
		user_new.setJobactivity(rs.getString("jobactivity"));
		user_new.setJoblevel(rs.getString("joblevel"));
		user_new.setSeclevel(rs.getString("seclevel"));
		user_new.setUserDepartment(Util.getIntValue(rs.getString("departmentid"), 0));
		user_new.setUserSubCompany1(Util.getIntValue(rs.getString("subcompanyid1"), 0));
		user_new.setUserSubCompany2(Util.getIntValue(rs.getString("subcompanyid2"), 0));
		user_new.setUserSubCompany3(Util.getIntValue(rs.getString("subcompanyid3"), 0));
		user_new.setUserSubCompany4(Util.getIntValue(rs.getString("subcompanyid4"), 0));
		user_new.setManagerid(rs.getString("managerid"));
		user_new.setAssistantid(rs.getString("assistantid"));
		user_new.setPurchaselimit(rs.getString("purchaselimit"));
		user_new.setCurrencyid(rs.getString("currencyid"));
		user_new.setLastlogindate(rs.getString("currentdate"));
		user_new.setLogintype("1");
		user_new.setAccount(rs.getString("account"));
		user_new.setLoginip(request.getRemoteAddr());
		request.getSession(true).setMaxInactiveInterval(60 * 60 * 24);
		request.getSession(true).setAttribute("weaver_user@bean", user_new);
		//多帐号登陆
		if (user_new.getUID() != 1) {  //is not sysadmin
			weaver.login.VerifyLogin VerifyLogin = new weaver.login.VerifyLogin();
			java.util.List accounts = VerifyLogin.getAccountsById(user_new.getUID());
			request.getSession(true).setAttribute("accounts", accounts);
		}
		request.getSession(true).setAttribute("moniter", new OnLineMonitor("" + user_new.getUID(),user_new.getLoginip()));
		Util.setCookie(response, "loginfileweaver", "/login/Login.jsp", 172800);
		Util.setCookie(response, "loginidweaver", ""+user_new.getUID(), 172800);
		Util.setCookie(response, "languageidweaver", languageidweaver, 172800);
		
		Map logmessages=(Map)application.getAttribute("logmessages");
		if(logmessages==null){
			logmessages=new HashMap();
			logmessages.put(""+user_new.getUID(),"");
			application.setAttribute("logmessages",logmessages);
		}
		request.getSession(true).setAttribute("logmessage",getLogMessage(user_new.getUID()+""));
		
		//处理浏览器是否为IE start
		String isIE = Util.null2String(request.getParameter("isie"));
		String agent = request.getHeader("user-agent");
		if(agent.indexOf("rv:11") == -1 && agent.indexOf("MSIE") == -1){
			isIE = "false";
		}
		if(agent.indexOf("rv:11") > -1 && agent.indexOf("Mozilla") > -1){
			isIE = "true";
		}
		if(!isIE.equals("false")){
			isIE = "true";
		}
		request.getSession(true).setAttribute("browser_isie",isIE);
		//处理浏览器是否为IE end

		char separater = Util.getSeparator();
		Calendar today = Calendar.getInstance();
		String LoginDate = Util.add0(today.get(Calendar.YEAR), 4) + "-" + Util.add0(today.get(Calendar.MONTH) + 1, 2) + "-" + Util.add0(today.get(Calendar.DAY_OF_MONTH), 2);
		rs.execute("HrmResource_UpdateLoginDate", rs.getString("id") + separater + LoginDate);

		//登录日志
		weaver.systeminfo.SysMaintenanceLog log1 = new  weaver.systeminfo.SysMaintenanceLog();
		log1.resetParameter();
		log1.setRelatedId(rs.getInt("id"));
		log1.setRelatedName((rs.getString("firstname") + " " + rs.getString("lastname")).trim());
		log1.setOperateType("6");
		log1.setOperateDesc("");
		log1.setOperateItem("60");
		log1.setOperateUserid(rs.getInt("id"));
		log1.setClientAddress(request.getRemoteAddr());
		try {
			log1.setSysLogInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}else{
		user_new = user;
	}

	String gopage = "/wui/main.jsp";
	if("workflow".equals(type)){
		gopage = "/wui/main.jsp?reqType=workflowCenter";
	}else if("threeheavy".equals(type)){
		
	}else if("missive".equals(type)){
		
	}else if("vot".equals(type)){
	
	//查看流程
	}else if("cc".equals(type)){
		gopage = "/workflow/request/ViewRequest.jsp?requestid="+requestid;
	//查看报销
	}else if("bx".equals(type)){
		gopage = "/workflow/request/PrintRequest.jsp?isFinanceAudit=1&isprint=1&fromFlowDoc=1&requestid="+requestid;
	//新增出差审批
	}else if("addcc".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=861";
	//新增差旅报销
	}else if("addclbx".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=381";
	//新增流程页面
	}else if("addworkflow".equals(type)){
		gopage = "/wui/main.jsp?reqType=workflowNew";
	//新增销售公司出差审批
	}else if("addcd".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=1644";
	//新增标准合同
	}else if("addbzht".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=2224";
	//新增一般合同
	}else if("addybht".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=841";
	//新增特别合同
	}else if("addtbht".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=1222";
	//新增财务审批表TEST
	}else if("addfinance_test".equals(type)){//172.18.67.39
		gopage = "/workflow/request/AddRequest.jsp?workflowid=945";
	//新增财务审批表
	}else if("addfinance".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=2604";
	//新增采购申请单TEST
	}else if("addpurchase_test".equals(type)){//172.18.67.39
		gopage = "/workflow/request/AddRequest.jsp?workflowid=123";
	//新增采购申请单
	}else if("addpurchase".equals(type)){
		gopage = "/workflow/request/AddRequest.jsp?workflowid=2624";
	}else if("xmxx".equals(type)){
		gopage = "/formmode/view/AddFormMode.jsp?type=0&modeId=101&formId=-72&billid="+billid+"&opentype=0&customid=101&viewfrom=fromsearchlist";
	}	
	response.sendRedirect(gopage);
	return;
}else{
	//跳转到错误页面
	msg = "OA不存在该用户";
	response.sendRedirect("/login/Login.jsp");
	return;
}
%>